package com.sskj.asset.ui.activity;


import android.text.InputFilter;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hey.lib.HeySpinner;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.asset.R;
import com.sskj.asset.R2;
import com.sskj.asset.bean.CoinEntry;
import com.sskj.asset.presenter.CashActivityPresenter;
import com.sskj.common.base.App;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.util.NumberUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.ASSET_CASH)//兑换
public class CashActivity extends BaseActivity<CashActivityPresenter> {


    @BindView(R2.id.select_unit2)
    HeySpinner selectUnit2;
    @BindView(R2.id.select_unit1)
    HeySpinner selectUnit1;
    @BindView(R2.id.exchange_num)
    EditText exchangeNum;
    @BindView(R2.id.unit1)
    TextView unit1;
    @BindView(R2.id.asset_num)
    TextView assetNum;
    @BindView(R2.id.unit2)
    TextView unit2;
    @BindView(R2.id.password)
    EditText password;
    @BindView(R2.id.submit)
    Button submit;
    @BindView(R2.id.ivTo)
    ImageView ivTo;
    @BindView(R2.id.ivFrom)
    ImageView ivFrom;

    private double rate;

    private String bid;
    private String ex_coin;
    private String min;
    private String max;

    @Override
    protected int getLayoutId() {
        return R.layout.asset_activity_cash;
    }

    @Override
    public CashActivityPresenter getPresenter() {
        return new CashActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.asset_cashActivity1));
        setRightTitle(App.INSTANCE.getString(R.string.asset_cashActivity2), v -> {
            ARouter.getInstance().build(RConfig.ASSET_CASH_RECORD).navigation();
        });

        RxTextView.afterTextChangeEvents(exchangeNum)
                .subscribe(textViewAfterTextChangeEvent -> {
                    calcExchange();

                });

        exchangeNum.setFilters(new InputFilter[]{new MoneyValueFilter(4)});
        //兑换
        ClickUtil.click(submit, () -> {

            if (TextUtils.isEmpty(exchangeNum.getText()) || Double.parseDouble(exchangeNum.getText().toString()) == 0) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.asset_cashActivity3));
                return;
            }
            if (TextUtils.isEmpty(password.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.asset_cashActivity4));
                return;
            }
            if (Double.valueOf(exchangeNum.getText().toString()) > Double.valueOf(max)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.asset_cashActivity5) + max);
                return;
            }
            if (Double.valueOf(exchangeNum.getText().toString()) < Double.valueOf(min)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.asset_cashActivity6) + min);
                return;
            }
            mPresenter.exchange(bid, ex_coin, exchangeNum.getText().toString(), password.getText().toString());
        });
    }

    private void calcExchange() {
        if (!TextUtils.isEmpty(exchangeNum.getText())) {
            Double num = Double.parseDouble(exchangeNum.getText().toString()) * rate;
            assetNum.setText(NumberUtil.keepMax4(num));
        } else {
            assetNum.setText("0");
        }
    }

    @Override
    protected void initData() {
        //获取可兑换币种
        mPresenter.getExchangeCoinList();
    }

    /**
     * 币种的兑换列表
     *
     * @param data
     */
    public void setExchangeCoinData(List<CoinEntry> data) {
        List<String> coinList = new ArrayList<>();
        for (CoinEntry coin : data) {
            coinList.add(coin.getPname());
        }
        selectUnit2.setOnSelectListener(position -> {
            unit2.setText(data.get(position).getPname());
            rate = Double.valueOf(data.get(position).getHuilv());
            ex_coin = data.get(position).getPid();
            ivTo.setImageResource(CoinEnum.getByCode(data.get(0).getPname()).getRes());
            exchangeNum.setText("");


        });
        ivTo.setImageResource(CoinEnum.getByCode(data.get(0).getPname()).getRes());

        exchangeNum.setText("");
        selectUnit2.attachData(coinList);

    }

    /**
     * 可兑换币种
     *
     * @param data
     */
    public void setExchangeAbleList(List<CoinEntry> data) {
        List<String> coinList = new ArrayList<>();
        for (CoinEntry coin : data) {
            coinList.add(coin.getBname());
        }
        selectUnit1.setOnSelectListener(position -> {
            mPresenter.getCoinList(data.get(position).getBid());
            unit1.setText(data.get(position).getBname());
            bid = data.get(position).getBid();
            ivFrom.setImageResource(CoinEnum.getByCode(data.get(position).getBname()).getRes());
            exchangeNum.setText("");
            min = data.get(position).getMinnums();
            max = data.get(position).getMaxnums();

        });
        exchangeNum.setText("");
        min = data.get(0).getMinnums();
        max = data.get(0).getMaxnums();
        bid = data.get(0).getBid();
        ivFrom.setImageResource(CoinEnum.getByCode(data.get(0).getBname()).getRes());
        selectUnit1.attachData(coinList);
    }

    public void submitSuccess(boolean isSuccess) {

        if (isSuccess) {
            password.getText().clear();
            exchangeNum.setText("");
            ARouter.getInstance().build(RConfig.ASSET_CASH_RECORD).navigation();

        }
    }

}
